﻿@inherits AdminCompontentBase
<MForm Model="_model" @ref="Form" EnableValidation EnableI18n Class="my-3">
    <MRow Class="row-box">
        <MCol Md="12">
            <STextField @bind-Value="_model.CorpId" Required
                        Label="@T("WeixinWorkChannelOptions.CorpId")"
                        Hint="@T("ChannelOptions.Tips")">
            </STextField>
        </MCol>
        <MCol Md="12">
            <STextField @bind-Value="_model.CorpSecret" Required
                        Label="@T("WeixinWorkChannelOptions.CorpSecret")"
                        Hint="@T("ChannelOptions.Tips")"
                        Type="@(!PasswordView?"password":_passwordShow ? "text" : "password")"
                        AppendIcon="@(!PasswordView?"":_passwordShow ? "mdi-eye" : "mdi-eye-off")" OnAppendClick="()=>_passwordShow = !_passwordShow">
            </STextField>
        </MCol>
        <MCol Md="12">
            <STextField @bind-Value="_model.AgentId" Required
                        Label="@T("WeixinWorkChannelOptions.AgentId")"
                        Hint="@T("ChannelOptions.Tips")">
            </STextField>
        </MCol>
    </MRow>
</MForm>

@code {
    [Parameter]
    public ExtraPropertyDictionary Value { get; set; } = new();

    [Parameter]
    public EventCallback<ExtraPropertyDictionary> ValueChanged { get; set; }

    [Parameter]
    public bool PasswordView { get; set; }

    public MForm Form { get; set; } = default!;

    private WeixinWorkChannelOptions _model = new();
    private bool _passwordShow;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            _model = ExtensionPropertyHelper.ConvertToType<WeixinWorkChannelOptions>(Value);
        }
        await base.OnAfterRenderAsync(firstRender);
    }

    public async Task HandleChangeAsync()
    {
        Value = ExtensionPropertyHelper.ConvertToExtraProperty(_model);
        await ValueChanged.InvokeAsync(Value);
    }
}
